package com.sczmgk.track.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

    ////////////////////////////乐政2.0数据表start///////////////////
    /**
     * 用户信息表
     */
    public static final String V2_USER_INFO_TABLE = "user_info";
    /**
     * 缓存用户信息的表
     */
    public static final String V2_USER_CACHE_INFO_TABLE = "user_cache_info_table";

    ///////////////////////////乐政2.0数据表end/////////////////////


    /**
     * 数据版本号
     */
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "oms_message.db";
    public static final String TABLE_ID = "_id";

    /**
     * 同步锁对象
     */
    public static final Object lockedObject = new Object();

    /**
     * 数据库核心对象
     */
    private static DBHelper dbHelper = null;


    private DBHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);

//        MyLog.d(TAG, "当前使用数据库=" + getLoginUserName() + "_" + DATABASE_NAME);
    }
    public static final String USERID = "userid";
    /**
     * 用户表
     */
    public static final String TABLE_USER = "oms_table_user";

    /**
     * 用户id
     */
    public static final String USER_NAME = "user_name";

    /**
     * 用户手机
     */
    public static final String USER_PHONE = "user_phone";

    /**
     * 用户头像地址
     */
    public static final String USER_HEAD_PATH = "user_head_path";

    /**
     * 头像本地路径
     */
    public static final String USER_IMAGELOCALPATH = "user_imagelocalpath";

    /**
     * 部门
     */
    public static final String USER_DEPARTMENT = "user_department";

    /**
     * 工号
     */
    public static final String USER_JOB_NUMBER = "user_job_number";

    /**
     * imei
     */
    public static final String USER_IMEI = "user_imei";

    /**
     * 当前手机imei绑定状态 0=验证正确  1=手机已更换  2=首次激活
     */
    public static final String USER_STATUS = "user_status";


    @Override
    public void onCreate(SQLiteDatabase db) {
           createDB(db);

    }

    private void createDB(SQLiteDatabase db){

        //用户表
       String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_USER + " (" + TABLE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + USERID + " TEXT,"
                + USER_NAME + " TEXT,"
                + USER_PHONE + " TEXT,"
                + USER_HEAD_PATH + " TEXT,"
                + USER_DEPARTMENT + " TEXT,"
                + USER_JOB_NUMBER + " TEXT,"
                + USER_IMEI + " TEXT,"
                + USER_STATUS + " TEXT,"
                + USER_IMAGELOCALPATH + " TEXT)";
        db.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        for (int i = oldVersion; i < newVersion; i++) {
            switch (i) {
                case 1:
                    version1ToVersion2(db);
                    break;
                case 2:
                    version2ToVersion3(db);
                    break;
                case 3:
                    version3ToVersion4(db);
                    break;
            }
        }
    }

    /**
     * 增加区域表,功能表(存图片路径)
     *
     * @param db
     */
    private void version1ToVersion2(SQLiteDatabase db) {
        //不处理
    }

    /**
     * 给搜索列表加入用户id
     *
     * @param db
     */
    private void version2ToVersion3(SQLiteDatabase db) {
        //不处理
    }

    /**
     * 删除2.0版本数据库
     *
     * @param db
     */
    private void version3ToVersion4(SQLiteDatabase db) {
        //TODO
        //1.删除之前的数据库
//        FileUtil.clearAllData(context);
        String sql = "drop table "+V2_USER_INFO_TABLE;
        db.execSQL(sql);
        sql = "drop table "+V2_USER_CACHE_INFO_TABLE;
        db.execSQL(sql);
        createDB(db);
    }

    /**
     * 关闭资源
     *
     * @param db
     * @param cursor
     */
    public static void closeAll(SQLiteDatabase db, Cursor cursor) {
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        if (db != null) {
            // db.close();
        }
    }

    /**
     * 销毁dbHelper
     */
    public static void destoryDBHelper() {
        synchronized (lockedObject) {
            if (dbHelper != null) {
                dbHelper.close();
                dbHelper = null;
            }
        }
    }

    /**
     * 获取数据库核心类单例对象
     *
     * @param context
     * @return
     */
    public static DBHelper getInstance(Context context) {
        synchronized (DBHelper.lockedObject) {
            if (dbHelper == null) {
                dbHelper = new DBHelper(context);
            }
            return dbHelper;
        }
    }
}
